package com.zhuo.mp.config;

import com.baomidou.mybatisplus.core.parser.ISqlParser;
import com.baomidou.mybatisplus.extension.parsers.BlockAttackSqlParser;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PerformanceInterceptor;
import com.baomidou.mybatisplus.extension.plugins.SqlExplainInterceptor;
import com.zhuo.mp.injectors.MySqlInjector;
import com.zhuo.mp.plugins.MyInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import java.util.ArrayList;
import java.util.List;

@Configuration
@MapperScan("com.zhuo.mp.mapper")   //设置mapper接口的扫描包
public class MybatisPlusConfig {
    //配置分页插件
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        return new PaginationInterceptor();
    }

    @Bean   //注入自定义拦截器
    public MyInterceptor myInterceptor(){
        return new MyInterceptor();
    }

    //SQL分析插件
    @Bean
    public SqlExplainInterceptor sqlExplainInterceptor(){

        SqlExplainInterceptor sqlExplainInterceptor = new SqlExplainInterceptor();

        List<ISqlParser> list = new ArrayList<>();
        list.add(new BlockAttackSqlParser());//全表更新删除的阻断器

        sqlExplainInterceptor.setSqlParserList(list);
        sqlExplainInterceptor.setSqlParserList(list);

        return sqlExplainInterceptor;
    }

    //性能分析插件
    @Bean
    public PerformanceInterceptor performanceInterceptor(){
        PerformanceInterceptor performanceInterceptor = new PerformanceInterceptor();
        performanceInterceptor.setMaxTime(1000);//最大执行时间，单位ms
        performanceInterceptor.setFormat(true);//对输出的SQL做格式化，默认为false
        return performanceInterceptor;
    }

    /**
     * 注入自定义的sql注入器
     * @return
     */
    @Bean
    public MySqlInjector mySqlInjector(){
        return new MySqlInjector();
    }
}
